package com.whfp.afs330h_sdk.utils;


import android.util.Log;

import java.util.HashMap;
import java.util.Map;

public class CalculateCodeUtils {

    private static CalculateCodeUtils calculateCodeUtils;

    //起始位
    private static String QSW;
    //检测技术
    private static Map<String, String> MAP_JCJS;
    //检测项目
    private static Map<String, String> MAP_JCXM;
    //生产年份
    private static Map<String, String> MAP_SCNF;
    //生产月份
    private static Map<String, String> MAP_SCYF;
    //生产批号
    private static Map<String, String> MAP_SCPH;

    public static CalculateCodeUtils getInstance() {
        if (calculateCodeUtils == null) {
            calculateCodeUtils = new CalculateCodeUtils();
            init();
        }
        return calculateCodeUtils;
    }

    /**
     * 初始化
     */
    private static void init() {
        //起始位（采用了AES加密）
        QSW = "DEwf3oFFu6EML7A2xWsX+A==";
        //检测技术
        MAP_JCJS = new HashMap<>();
        MAP_JCJS.put("e4b88ae8bdac", "303031");

        //这两个都是荧光，扫码到的字符串“荧光”转为16进制会得到不同结果，故此写两个
        MAP_JCJS.put("efbbbfe88da7e58589", "303130");
        MAP_JCJS.put("e88da7e58589","303130");

        MAP_JCJS.put("e883b6e4bd93e98791", "303131");
        MAP_JCJS.put("e9a284e79599", "303030");
        //检测项目
        MAP_JCXM = new HashMap<>();
        MAP_JCXM.put("e586b0e6af92", "30303031");
        MAP_JCXM.put("e59097e595a1", "30303130");
        MAP_JCXM.put("4be7b289", "30303131");
        MAP_JCXM.put("e5a4a7e9babb", "30313030");
        MAP_JCXM.put("e58fafe58da1e59ba0", "30313031");
        MAP_JCXM.put("e69187e5a4b4e4b8b8", "30313130");
        MAP_JCXM.put("e59097e586b04be4b889e88194", "30313131");
        MAP_JCXM.put("e7be8ee6b299e985ae", "31303030");
        MAP_JCXM.put("e5a4a7e9babbe7b4a0", "31303031");
        MAP_JCXM.put("e88aace5a4aae5b0bc", "31303130");
        MAP_JCXM.put("e794b2e58da1e8a5bfe985ae", "31303131");
        MAP_JCXM.put("e88bafe4ba8ce6b0aee58d93", "31313030");
        MAP_JCXM.put("e5b7b4e6af94e5a6a5", "31313031");
        MAP_JCXM.put("e69bb2e9a9ace5a49a", "31313130");
        MAP_JCXM.put("e59097e586b0e4ba8ce88194", "31313131");
        //生产年份
        MAP_SCNF = new HashMap<>();
        MAP_SCNF.put("32303230", "30303031");
        MAP_SCNF.put("32303231", "30303130");
        MAP_SCNF.put("32303232", "30303131");
        MAP_SCNF.put("32303233", "30313030");
        MAP_SCNF.put("32303234", "30313031");
        MAP_SCNF.put("32303235", "30313130");
        MAP_SCNF.put("32303236", "30313131");
        MAP_SCNF.put("32303237", "31303030");
        MAP_SCNF.put("32303238", "31303031");
        MAP_SCNF.put("32303239", "31303130");
        MAP_SCNF.put("32303330", "31303131");
        MAP_SCNF.put("32303331", "31313030");
        MAP_SCNF.put("32303332", "31313031");
        MAP_SCNF.put("32303333", "31313130");
        MAP_SCNF.put("32303334", "31313131");
        //生产月份
        MAP_SCYF = new HashMap<>();
        MAP_SCYF.put("3031", "30303031");
        MAP_SCYF.put("3032", "30303130");
        MAP_SCYF.put("3033", "30303131");
        MAP_SCYF.put("3034", "30313030");
        MAP_SCYF.put("3035", "30313031");
        MAP_SCYF.put("3036", "30313130");
        MAP_SCYF.put("3037", "30313131");
        MAP_SCYF.put("3038", "31303030");
        MAP_SCYF.put("3039", "31303031");
        MAP_SCYF.put("3130", "31303130");
        MAP_SCYF.put("3131", "31303131");
        MAP_SCYF.put("3132", "31313030");
        //生产批号
        MAP_SCPH = new HashMap<>();
        MAP_SCPH.put("3031", "30303031");
        MAP_SCPH.put("3032", "30303130");
        MAP_SCPH.put("3033", "30303131");
        MAP_SCPH.put("3034", "30313030");
        MAP_SCPH.put("3035", "30313031");
        MAP_SCPH.put("3036", "30313130");
        MAP_SCPH.put("3037", "30313131");
        MAP_SCPH.put("3038", "31303030");
        MAP_SCPH.put("3039", "31303031");
        MAP_SCPH.put("3130", "31303130");
        MAP_SCPH.put("3131", "31303131");
        MAP_SCPH.put("3132", "31313030");
        MAP_SCPH.put("3133", "31313031");
        MAP_SCPH.put("3134", "31313130");
        MAP_SCPH.put("3135", "31313131");
    }


    /**
     * 条码计算
     *
     * @param str
     * @return
     */
    public String calculateCode(String str) throws Exception {
        getInstance();
        String[] s = str.split("-");
        String jcjs = RxHexTool.str2HexStr(s[0]);      //检测技术
        String jcxm = RxHexTool.str2HexStr(s[1]);      //检测项目
        String scnf = RxHexTool.str2HexStr(s[2]);      //生产年份
        String scyf = RxHexTool.str2HexStr(s[3]);      //生产月份
        String scph = RxHexTool.str2HexStr(s[4]);      //生产批号
        String zzsxm = RxHexTool.str2HexStr(s[5]);     //四位自增顺序码


        Log.i("TAG", "检测技术："+jcjs+"    哈哈"+s[0]);
        //检测技术
        for (Map.Entry<String, String> entry : MAP_JCJS.entrySet()) {
            String key = entry.getKey().toUpperCase();
            if (key.equals(jcjs)) {
                jcjs = entry.getValue();
                break;
            }
        }
        Log.i("TAG", "检测项目："+jcxm+"    呵呵"+s[1]);
        //检测项目
        for (Map.Entry<String, String> entry : MAP_JCXM.entrySet()) {
            String key = entry.getKey().toUpperCase();
            if (key.equals(jcxm)) {
                jcxm = entry.getValue();
                break;
            }
        }
        Log.i("TAG", "生产年份："+scnf+"    嚯嚯"+s[2]);
        //生产年份
        for (Map.Entry<String, String> entry : MAP_SCNF.entrySet()) {
            String key = entry.getKey().toUpperCase();
            if (key.equals(scnf)) {
                scnf = entry.getValue();
                break;
            }
        }
        Log.i("TAG", "生产月份："+scyf+"    嘻嘻"+s[3]);
        //生产月份
        for (Map.Entry<String, String> entry : MAP_SCYF.entrySet()) {
            String key = entry.getKey().toUpperCase();
            if (key.equals(scyf)) {
                scyf = entry.getValue();
                break;
            }
        }
        Log.i("TAG", "生产批号："+scph+"    哼哼"+s[4]);
        //生产批号
        for (Map.Entry<String, String> entry : MAP_SCPH.entrySet()) {
            String key = entry.getKey().toUpperCase();
            if (key.equals(scph)) {
                scph = entry.getValue();
                break;
            }
        }
        String binCode = AESUtils.decryptQSW(QSW) + RxHexTool.hexStringToString(jcjs) + RxHexTool.hexStringToString(jcxm) +
                RxHexTool.hexStringToString(scnf) + RxHexTool.hexStringToString(scyf) + RxHexTool.hexStringToString(scph);
        Log.i("TAG", "二进制是："+binCode);
        String hexCode = RxHexTool.binToHex(binCode);
        Log.i("TAG", "十六进制是："+hexCode);
        String finalCode = hexCode + RxHexTool.hexStringToString(zzsxm).trim();
        Log.i("TAG", "最终："+finalCode);
        return finalCode;
    }
}
